package com.rtx.wcs.std.handle.ristowcs.bztask;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rtx.edi.model.api.bo.ApiMappingInfoBo;
import com.rtx.edi.platform.synctask.service.ConvertService;
import com.rtx.wcs.std.bean.po.EdiTaskDownLog;
import com.rtx.wcs.std.bean.po.RtxSowdetail;
import com.rtx.wcs.std.handle.ristowcs.AbsBZDownHandle;
import com.rtx.wcs.std.service.RtxSowdetailService;
import com.rtx.wms.std.common.bean.Codelkup;
import com.rtx.wms.std.common.service.CommonService;
import com.rtx.wms.std.so.bean.Pickdetail;
import com.rtx.wms.std.so.service.PickdetailService;

/**
 * 播种任务下发
 * @author Administrator
 *
 */
@Service("BZ-TASK")
public class AbsBZTaskDownHandle extends AbsBZDownHandle<RtxSowdetail>{
	
	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	RtxSowdetailService RtxSowdetailService;
	
	@Autowired
	CommonService commonService;
	
	@Autowired
	ConvertService convertService;
	
	@Autowired
	PickdetailService pickdetailService;
	
	@Override
	protected void updStatus(RtxSowdetail o,String response) {
		
		//处理响应
		Map<String,String> resultMap = handleResponse(o,response);
		
		if(!"PS".equals(resultMap.get("editflag"))) {
			throw new RuntimeException("wcs返回错误-"+resultMap.get("message"));
		}
	}
	
	/**
	 * 解析返回报文
	 * @param data
	 */
	protected Map<String, String> handleResponse(RtxSowdetail o, String response) {
		Map<String,String> resultMap = new HashMap<>();
		resultMap.put("editflag", "");//PS为成功 PE为失败
		resultMap.put("message", "");
		resultMap.put("data", "");
		resultMap.put("status", "");
		
		try {
			JSONObject res = JSON.parseObject(response);
			if("0".equals(res.getString("code"))) {
				resultMap.put("editflag", "PS");
			}else {
				resultMap.put("editflag", "PE");
			}
			resultMap.put("message", res.getString("msg"));
		} catch (Exception e) {
			logger.error("",e);
			resultMap.put("message", response);
			resultMap.put("editflag", "PE");
		}
		return resultMap;
	}


	//[{"whseid":"仓库号","sowdevice":"播种设备","orderkey":"单号","seqno":"格口号","pickdetailkey":"pickdetailkey","sideCode":"亮灯面 1投料面 2取料面","color":"颜色"}]
	@Override
	protected List<RtxSowdetail> queryToSendDataByRISWmsCondition(String request) {
		List<RtxSowdetail> allList = new ArrayList<RtxSowdetail>();
		List<JSONObject> requestList = JSON.parseArray(request, JSONObject.class);
		
		for(JSONObject data:requestList) {
//			JSONObject data = JSON.parseObject(request);
			String whseid = data.getString("whseid");
			
			LambdaQueryWrapper<RtxSowdetail> qw = Wrappers.lambdaQuery();
			qw.eq(RtxSowdetail::getSowdevice, data.getString("sowdevice"));
			qw.eq(RtxSowdetail::getOrderkey, data.getString("orderkey"));
			qw.eq(RtxSowdetail::getSeqno, data.getString("seqno"));
			List<RtxSowdetail> list = RtxSowdetailService.queryByCondition(whseid, qw);
			
			LambdaQueryWrapper<Pickdetail> pickdeqw = Wrappers.lambdaQuery();
			pickdeqw.eq(Pickdetail::getPickdetailkey, data.getString("pickdetailkey"));
			List<Pickdetail> pickdetailList = pickdetailService.queryByCondition(whseid, pickdeqw);
			if(!CollectionUtils.isEmpty(requestList)) {
				list.forEach(temp->temp.setPickdetail(pickdetailList.get(0)));
			}
			
			LambdaQueryWrapper<Codelkup> cqw = Wrappers.lambdaQuery();
			cqw.eq(Codelkup::getListname, "RTXSOWPOSITION");
			cqw.eq(Codelkup::getUdf2, data.getString("seqno"));
			cqw.eq(Codelkup::getUdf1, data.getString("sowdevice"));
			List<Codelkup> configList = commonService.selectCodelkupByWrapper(whseid, cqw);
			
			list.forEach(entity->{
				entity.setConfig(configList.get(0));
				entity.setSowdevice(data.getString("sowdevice"));
				entity.setColor(data.getString("color"));
				entity.setSideCode(data.getString("sideCode"));
				});
			allList.addAll(list);
		}

		
		return allList;
	}


	@Override
	protected String getapiname(String request) {
		return "播种任务";
	}
	


	@Override
	protected void mappingReqToLog(RtxSowdetail data, EdiTaskDownLog log) {
		log.setType("bztask");
		log.setSowdevice(data.getSowdevice());
		log.setSeqno(data.getSeqno());
		log.setOriginalqty(data.getOriginalqty());
	}

	@Override
	protected JSONObject converWcsRequest(RtxSowdetail t,String mappingcode) {
		String source = JSON.toJSONString(t);
		String targetjson = convertService.ConvertByMappingCodeAndSource(mappingcode, source);
		return JSON.parseObject(targetjson);
	}

	@Override
	protected String getEdiRequestURL(String mappingcode) {
		ApiMappingInfoBo bo = convertService.getConfigByApimappingcode(mappingcode);
		return bo.getTargetUrl();
	}
	
	
}
